package org.abc.fund.controller.funds;

import lombok.Data;
import org.abc.fund.entity.funds.FundPortfolioItem;
import org.abc.fund.service.funds.FundPortfolioItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/portfolio/item")
public class FundPortfolioItemController {

    @Autowired
    private FundPortfolioItemService fundPortfolioItemService;

    // ✅ 查询某个组合下的所有基金项
    @GetMapping("/{portfolioId}")
    public List<FundPortfolioItem> getItems(@PathVariable Long portfolioId) {
        return fundPortfolioItemService.getItemsByPortfolioId(portfolioId);
    }

    // ✅ 添加单个基金项（备用）
    @PostMapping("/add")
    public FundPortfolioItem addItem(@RequestBody FundPortfolioItem item) {
        return fundPortfolioItemService.addItem(item);
    }

    // ✅ 根据 ID 删除单个基金项（备用）
    @DeleteMapping("/delete/{id}")
    public void deleteItem(@PathVariable Long id) {
        fundPortfolioItemService.deleteItemById(id);
    }

    // ✅ 批量删除某个组合下的多个基金
    @DeleteMapping
    public void deleteItems(@RequestBody DeleteFundRequest request) {
        fundPortfolioItemService.deleteByPortfolioIdAndCodes(request.getPortfolioId(), request.getFundCodes());
    }

    // ✅ DTO：批量删除请求结构
    @Data
    public static class DeleteFundRequest {
        private Long portfolioId;
        private List<String> fundCodes;
    }

    // ✅ 删除某个组合中的单个基金
    @DeleteMapping("/remove")
    public void deleteItemByPortfolioIdAndFundCode(
            @RequestParam Long portfolioId,
            @RequestParam String fundCode
    ) {
        fundPortfolioItemService.deleteByPortfolioIdAndFundCode(portfolioId, fundCode);
    }


}
